package utils.style;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

public class WishExcelStyle
{
	public static WriteCellStyle bold = null;

	public static WriteCellStyle getWriteCellStyleBold()
	{
		if (bold == null)
		{
			bold = new WriteCellStyle();
			bold.setHorizontalAlignment(HorizontalAlignment.CENTER);
			bold.setVerticalAlignment(VerticalAlignment.CENTER);
			bold.setBorderLeft(BorderStyle.THIN);
			bold.setBorderRight(BorderStyle.THIN);
			bold.setBorderTop(BorderStyle.THIN);
			bold.setBorderBottom(BorderStyle.THIN);
			WriteFont contentWriteFont = new WriteFont();
			contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
			contentWriteFont.setBold(true);
			contentWriteFont.setFontName("宋体");
			contentWriteFont.setFontHeightInPoints((short) 10);
			bold.setWriteFont(contentWriteFont);
		}
		return bold;
	}

	private static HorizontalCellStyleStrategy horizontalCellStyleStrategy = null;

	public static HorizontalCellStyleStrategy getDefaultStyle()
	{
		if (horizontalCellStyleStrategy == null)
		{
			WriteCellStyle headWriteCellStyle = new WriteCellStyle();

			WriteFont headWriteFont = new WriteFont();
			headWriteFont.setColor(IndexedColors.BLACK.getIndex());
			headWriteFont.setBold(true);
			headWriteFont.setFontName("宋体");
			headWriteFont.setFontHeightInPoints((short) 10);

			// headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
			headWriteCellStyle.setWriteFont(headWriteFont);
			headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
			headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			// headWriteCellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
			headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
			headWriteCellStyle.setBorderRight(BorderStyle.THIN);
			headWriteCellStyle.setBorderTop(BorderStyle.THIN);
			headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
			// headWriteCellStyle.setFillBackgroundColor(IndexedColors.BLACK.index);
			headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.index);// 背景颜色
			// headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

			// 内容的策略
			WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
			contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
			contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
			contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
			contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
			contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
			WriteFont contentWriteFont = new WriteFont();
			contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
			contentWriteFont.setBold(false);
			contentWriteFont.setFontName("宋体");
			contentWriteFont.setFontHeightInPoints((short) 10);
			contentWriteCellStyle.setWriteFont(contentWriteFont);

			// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
			horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
		}

		return horizontalCellStyleStrategy;
	}

	private static WriteHandler headStyle = null;

	public static WriteHandler getHeadStyle()
	{
		if (headStyle == null)
		{
			headStyle = new AbstractCellStyleStrategy()
			{

				protected void setHeadCellStyle(CellWriteHandlerContext context)
				{
					if (!context.getHead())
					{
						return;
					}

					if (context.getRelativeRowIndex() == 0)
					{
						if (context.getHeadData().getHeadNameList().size() > 1)
						{
							context.getFirstCellData().getWriteCellStyle().getWriteFont().setFontHeightInPoints((short) 12);
						}
						context.getRow().setHeightInPoints((short) 28);
					}
					setHeadCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex());
				}

				@Override
				protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex)
				{
				}

				@Override
				protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex)
				{
				}
			};
		}
		return headStyle;
	}
}
